define(function(require, exports, module) {

	var $ = require("jquery1.7");

    var uploadify = require("uploadify");
	var utils = require("utils");
	var ctx = utils.constants.ctx;
	var backManagerService = require("bean/backManagerService");
	var ui = require("ui");
	
	$('#file_upload').uploadify({
		'buttonText':      "选择xls文件",
		'fileTypeExts':     "*.xls",
		'multi':            false,
		'queueID'  :       'uploadMessage',
	    'swf'      :        ctx+'/static-res/sea-modules/1.0/uploadify/uploadify.swf',
	    'uploader' :        ctx+'/pb-upload-json',
	    'onUploadSuccess' : function(file, data, response) {
//	    	console.log(data);
	        	       var json = eval("("+data+")");
//	        	       console.log(json.fileId);
	        	       $("#maskDiv").show();
        	    	   $("#uploadMessage").append('<div style="line-height:20px;">'+json.fileName+'已上传</div>')
        	    	   $("#uploadMessage").append('<div style="line-height:20px;">'+json.fileName+'正在导入..</div>')
	        	       importxls(json.id);
	        	    },
	    'onSelectError' :   function() {
		        alert('文件格式错误 ');
		    }
	        // Put your options here
	    });

	
	$('.uploadify-cancel').live("click",function(){
		var _this = $(this);
		var obj = _this.attr("idate");
		$('#file_upload').uploadify('cancel', obj);
	});
	
	var userList = "";
	
	var importxls = function (id){
		$.post(ctx+"/back/base/importxls",{"id":id},function(data){
			$("#uploadMessage").append('<div style="line-height:20px;">数据检测完成</div>')
			var errList = data.message.split(";")
			for(var err in errList){
				if(errList[err] == ''){
					continue;
				}
				if(errList[err].indexOf("行数据需要导入") > 0){
					$("#uploadMessage").append('<div style="line-height:20px;color:blue">'+errList[err]+'..</div>')
				} else {
					$("#uploadMessage").append('<div style="line-height:20px;color:red">'+errList[err]+'</div>')
				}
				
				var div = document.getElementById('uploadMessage');
				div.scrollTop = div.scrollHeight;
			}
			$("#uploadMessage").append('<div style="line-height:20px;">是否继续导入数据？</div>')
			var div = document.getElementById('uploadMessage');
			div.scrollTop = div.scrollHeight;
			userList = data.key;
			$("#importContinue").show();
			$("#importCancel").show();
			 
		});
	}
	
	
	$("#importContinue").bind("click",function(){
//		for(var i in userList){
//			var user = {
//	    			username : userList[i].username,
//	    			password : "111111",
//	    			nickname : userList[i].nickname,
//	    			email : userList[i].email,
//	    			phoneNumber : userList[i].phone,
//	    			approval : 1 
//	    		};
//			try {
//	    	 	backManagerService.userAdd(user);
//    	 	} catch (e){
//    	 		$("#uploadMessage").append('<div style="line-height:20px;">用户名：'+userList[i].username+'已存在</div>')
//    	 		continue ;
//    	 	}
//		}
		ui.mask("body").show("请稍候...");
		$.post(ctx+"/back/base/importxls/"+userList, function(data){
			ui.mask("body").hide();
			$("#uploadMessage").append('<div style="line-height:20px;">导入完成..</div>')
			$("#uploadMessage").append('<div style="line-height:20px;color:#0449be;">共导入'+data+'条记录.</div>')
			var div = document.getElementById('uploadMessage');
			div.scrollTop = div.scrollHeight;
			$("#importContinue").hide();
			$("#importCancel").hide();
			$("#importFinish").show();
		})
		
	});
	
	$("#download_template").click(function(){
		window.open(ctx + "/files/table/template.xls");
	})
	
	$("#importFinish,#importCancel").click(function(){
		window.location.reload();
	})
});